$OpenBSD: patch-src_tracker_tracker-process_c,v 1.2 2015/03/29 12:23:38 ajacoutot Exp $

From 95ddae1b224c75b37b2dbe5f1f6b4cb69ea79bdd Mon Sep 17 00:00:00 2001
From: Antoine Jacoutot <ajacoutot@gnome.org>
Date: Sat, 28 Mar 2015 17:56:44 +0100
Subject: openbsd: implement tracker_process_get_uid_for_pid()

--- src/tracker/tracker-process.c.orig	Tue Jan 27 00:58:43 2015
+++ src/tracker/tracker-process.c	Sat Mar 28 17:15:30 2015
@@ -26,6 +26,15 @@
 #include <glib.h>
 #include <glib/gi18n.h>
 
+#ifdef __OpenBSD__
+#include <sys/param.h>
+#include <sys/proc.h>
+#include <sys/sysctl.h>
+#include <fcntl.h>
+#include <kvm.h>
+#include <unistd.h>
+#endif
+
 #include "tracker-process.h"
 
 static TrackerProcessData *
@@ -134,6 +143,7 @@ tracker_process_get_uid_for_pid (const gchar  *pid_as_
 GSList *
 tracker_process_find_all (void)
 {
+#ifndef __OpenBSD__
 	GSList *pids, *l;
 	GSList *found_pids = NULL;
 	guint32 own_pid;
@@ -221,6 +231,50 @@ tracker_process_find_all (void)
 	g_slist_free (pids);
 
 	return g_slist_reverse (found_pids);
+#else /* ! __OpenBSD__ */
+	GSList *found_pids = NULL;
+	gchar **strv;
+	gchar *basename;
+	pid_t pid;
+	gint i, nproc;
+	gchar buf[_POSIX2_LINE_MAX];
+	struct kinfo_proc *plist, *kp;
+	kvm_t *kd;
+
+	if ((kd = kvm_openfiles (NULL, NULL, NULL, KVM_NO_FILES, buf)) == NULL)
+		return NULL;
+
+	if ((plist = kvm_getprocs (kd, KERN_PROC_ALL, 0, sizeof (*plist), &nproc)) == NULL)
+		return NULL;
+
+	for (i = 0, kp = plist; i < nproc; i++, kp++) {
+		if ((kp->p_flag & P_SYSTEM) != 0)
+			continue;
+		if ((strv = kvm_getargv (kd, kp, 0)) == NULL)
+			continue;
+
+		pid = kp->p_pid;
+
+		/* Don't return our own PID */
+		if (pid == getpid ())
+			continue;
+
+		/* Don't return PID we don't own */
+		if (kp->p_uid != getuid ())
+			continue;
+
+		basename = g_path_get_basename (strv[0]);
+
+		if ((g_str_has_prefix (basename, "tracker") ||
+		     g_str_has_prefix (basename, "lt-tracker"))) {
+			found_pids = g_slist_prepend (found_pids, process_data_new (basename, pid));
+		} else {
+			g_free (basename);
+		}
+	}
+
+	return g_slist_reverse (found_pids);
+#endif
 }
 
 gint
